# Minimum version required
cmake_minimum_required (VERSION 3.2)

# Project name
project (common_osqp)

# Some non-standard CMake modules
LIST(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/configure/cmake)
INCLUDE(FindPythonModule)
INCLUDE(Utils)

## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
  roscpp
)

set(IS_LINUX ON)

message(STATUS "Embedded is OFF")

option (PRINTING "Enable solver printing" ON)

option (PROFILING "Enable solver profiling (timing)" ON)

option (CTRLC "Enable user interrupt (Ctrl-C)" ON)

option (DFLOAT "Use float numbers instead of doubles" OFF)

# Use long integers for indexing
option (DLONG "Use long integers (64bit) for indexing" ON)
if (NOT (CMAKE_SIZEOF_VOID_P EQUAL 8))
	message(STATUS "Disabling long integers (64bit) on 32bit machine")
	set(DLONG OFF)
endif()
message(STATUS "Long integers (64bit) are ${DLONG}")

option (COVERAGE "Perform code coverage" OFF)

option (ENABLE_MKL_PARDISO "Enable MKL Pardiso solver" ON)
message(STATUS "MKL Pardiso: ${ENABLE_MKL_PARDISO}")

# Generate header file with the global options
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/configure/osqp_configure.h.in
               ${CMAKE_CURRENT_SOURCE_DIR}/include/osqp_configure.h
               NEWLINE_STYLE LF)

# Set Compiler flags
set(CMAKE_POSITION_INDEPENDENT_CODE ON)  # -fPIC

# Add specific flag for debug
set(CMAKE_C_FLAGS_DEBUG "-DDDEBUG")

if (NOT MSVC)
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3")
    set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0 -g")
    set(CMAKE_C_STANDARD_LIBRARIES "${CMAKE_C_STANDARD_LIBRARIES} -lm")      # Include math
    # Include real time library in linux
    if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
        set(CMAKE_C_STANDARD_LIBRARIES "${CMAKE_C_STANDARD_LIBRARIES} -lrt -ldl")
    endif()
endif (NOT MSVC)

# Set sources
set(
    osqp_src
    src/auxil.c
    src/cs.c
    src/ctrlc.c
    src/kkt.c
    src/lin_alg.c
    src/lin_sys.c
    src/osqp.c
    src/polish.c
    src/proj.c
    src/scaling.c
    src/util.c
)

set(
    osqp_headers
    include/auxil.h
    include/constants.h
    include/cs.h
    include/ctrlc.h
    include/glob_opts.h
    include/kkt.h
    include/lin_alg.h
    include/lin_sys.h
    include/osqp.h
    include/osqp_configure.h
    include/types.h
    include/polish.h
    include/proj.h
    include/scaling.h
    include/util.h
)

###################################
## catkin specific configuration ##
###################################
## The catkin_package macro generates cmake config files for your package
## Declare things to be passed to dependent projects
## INCLUDE_DIRS: uncomment this if your package contains header files
## LIBRARIES: libraries you create in this project that dependent projects also need
## CATKIN_DEPENDS: catkin_packages dependent projects also need
## DEPENDS: system dependencies of this project that dependent projects also need
catkin_package(
  INCLUDE_DIRS include
  LIBRARIES osqp
  CATKIN_DEPENDS roscpp
#  DEPENDS system_lib
)

# Create Static Library
# Add linear system solvers cumulative library
add_subdirectory(lin_sys)

# Static library
add_library (osqpstatic STATIC ${osqp_src} ${osqp_headers} ${linsys_solvers})
# Give same name to static library output
set_target_properties(osqpstatic PROPERTIES OUTPUT_NAME osqp)

# Include directories for linear system solvers
target_include_directories(osqpstatic PRIVATE ${linsys_solvers_includes})

# Declare include directories for the cmake exported target
target_include_directories(osqpstatic
                           PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
                                  "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}/osqp>")

# Create osqp shared library
# NB: Add all the linear system solvers here
add_library (osqp SHARED ${osqp_src} ${osqp_headers} ${linsys_solvers})

# Include directories for linear system solvers
target_include_directories(osqp PRIVATE ${linsys_solvers_includes})

# Declare include directories for the cmake exported target
target_include_directories(osqp
                            PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
                                    "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}/osqp>")
